import Vue from "vue";
import router from "./router";
import App from "./App.vue";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";

import { MessageBox } from "element-ui";

//导入样式
import "./assets/css/global.css";
import "./assets/fonts/iconfont.css";
import axios from "axios";

Vue.prototype.$axios = axios;

axios.defaults.withCredentials = true;

//axios请求头设置
axios.interceptors.request.use((config) => {
  config.headers.Authorization =
    // "Bearer " + window.sessionStorage.getItem("token");
    window.sessionStorage.getItem("token");
  return config;
});

// console.log(message);

axios.interceptors.response.use(
  (response) => {
    // console.log(response);
    if (response.data.meta != null && response.data.meta.code != 200) {
      ElementUI.Message({
        message: response.data.meta.msg,
        type: "error",
        showClose: true,
      });
    }
    return response;
  },
  (error) => {
    if (error.response) {
      if (error.response.status === 403) {
        MessageBox.alert(
          "抱歉！您的身份认证信息已过期！请重新登录！",
          "身份认证过期",
          { type: "error" }
        );
        window.setTimeout("window.location.href='/'", 1500);
      } else if (error.response.status === 400) {
        MessageBox.alert("出错啦！请联系管理员", "错误！", {
          type: "error",
          callback: function() {
            router.go(0);
          },
        });
      } else if (error.response.status === 500) {
        ElementUI.Message({
          message: error.response.statusText + "，请尝试刷新重试",
          type: "error",
          duration: 5000,
          showClose: true,
        });
      }
    }
    return Promise.reject(error.response.data);
  }
);

Vue.config.productionTip = false;
Vue.prototype.HOST = "/api";

Vue.use(ElementUI);
new Vue({
  router,
  render: (h) => h(App),
}).$mount("#app");
